<template>
  <!-- 角色表单弹窗 -->
  <el-dialog
    :close-on-click-modal="false"
    v-model="visibleDialog"
    :title="props.title"
    width="50%"
    draggable
    @close="closeDialog"
  >
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="100px">
      <el-form-item label="设备编号" prop="equipCode">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.equipCode"
          placeholder="请输入设备编号"
          clearable
        />

        <span v-else>{{ formData.equipCode }}</span>
      </el-form-item>
      <el-form-item label="设备名称" prop="equipName">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.equipName"
          placeholder="请输入设备名称"
          clearable
        />

        <span v-else>{{ formData.equipName }}</span>
      </el-form-item>
      <el-form-item label="设备型号" prop="equipModel">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.equipModel"
          placeholder="请输入设备型号"
          clearable
        />

        <span v-else>{{ formData.equipModel }}</span>
      </el-form-item>
      <el-form-item label="设备类型主键" prop="equipTypeId">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.equipTypeId"
          placeholder="请输入设备类型主键"
          clearable
        />

        <span v-else>{{ formData.equipTypeId }}</span>
      </el-form-item>
      <el-form-item label="设备类型编号" prop="equipTypeCode">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.equipTypeCode"
          placeholder="请输入设备类型编号"
          clearable
        />

        <span v-else>{{ formData.equipTypeCode }}</span>
      </el-form-item>
      <el-form-item label="设备类型名称" prop="equipTypeName">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.equipTypeName"
          placeholder="请输入设备类型名称"
          clearable
        />

        <span v-else>{{ formData.equipTypeName }}</span>
      </el-form-item>
      <el-form-item label="设备状态" prop="equipStatus">
        <el-select
          v-if="!props.isReadonly"
          v-model="formData.equipStatus"
          placeholder="请选择设备状态"
          clearable
        >
          <el-option
            v-for="item in dictionaryStore.filterDictList('EquipStatus')"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>

        <span v-else>{{
          dictionaryStore.filterDictName("EquipStatus", formData.equipStatus)
        }}</span>
      </el-form-item>
      <el-form-item label="设备状态名称" prop="equipStatusName">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.equipStatusName"
          placeholder="请输入设备状态名称"
          clearable
        />

        <span v-else>{{ formData.equipStatusName }}</span>
      </el-form-item>
      <el-form-item label="位置主键" prop="locationId">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.locationId"
          placeholder="请输入位置主键"
          clearable
        />

        <span v-else>{{ formData.locationId }}</span>
      </el-form-item>
      <el-form-item label="资产编码" prop="assetCode">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.assetCode"
          placeholder="请输入资产编码"
          clearable
        />

        <span v-else>{{ formData.assetCode }}</span>
      </el-form-item>
      <el-form-item label="公司id" prop="organizationId">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.organizationId"
          placeholder="请输入公司id"
          clearable
        />

        <span v-else>{{ formData.organizationId }}</span>
      </el-form-item>
      <el-form-item label="公司编号" prop="organizationCode">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.organizationCode"
          placeholder="请输入公司编号"
          clearable
        />

        <span v-else>{{ formData.organizationCode }}</span>
      </el-form-item>
      <el-form-item label="工厂id" prop="plantId">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.plantId"
          placeholder="请输入工厂id"
          clearable
        />

        <span v-else>{{ formData.plantId }}</span>
      </el-form-item>
      <el-form-item label="工厂编号" prop="plantCode">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.plantCode"
          placeholder="请输入工厂编号"
          clearable
        />

        <span v-else>{{ formData.plantCode }}</span>
      </el-form-item>
      <el-form-item label="工作中心id" prop="workCenterId">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.workCenterId"
          placeholder="请输入工作中心id"
          clearable
        />

        <span v-else>{{ formData.workCenterId }}</span>
      </el-form-item>
      <el-form-item label="工作中心编号" prop="workCenterCode">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.workCenterCode"
          placeholder="请输入工作中心编号"
          clearable
        />

        <span v-else>{{ formData.workCenterCode }}</span>
      </el-form-item>
      <el-form-item label="工序主键" prop="processId">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.processId"
          placeholder="请输入工序主键"
          clearable
        />

        <span v-else>{{ formData.processId }}</span>
      </el-form-item>
      <el-form-item label="工序编号" prop="processCode">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.processCode"
          placeholder="请输入工序编号"
          clearable
        />

        <span v-else>{{ formData.processCode }}</span>
      </el-form-item>
      <el-form-item label="是否停用" prop="enabled">
        <el-select
          v-if="!props.isReadonly"
          v-model="formData.enabled"
          placeholder="请选择是否停用"
          clearable
        >
          <el-option
            v-for="item in dictionaryStore.filterDictList('Enabled')"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>

        <span v-else>{{
          dictionaryStore.filterDictName("Enabled", formData.enabled)
        }}</span>
      </el-form-item>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button
          v-if="!props.isReadonly"
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >确 定</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "MDMEQUIPINFOADDORUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
  }
);

const rules = reactive({
  equipCode: [{ required: true, message: "请输入设备编号", trigger: "blur" }],
  equipName: [{ required: true, message: "请输入设备名称", trigger: "blur" }],
  equipModel: [{ required: true, message: "请输入设备型号", trigger: "blur" }],
  equipTypeId: [
    { required: true, message: "请输入设备类型主键", trigger: "blur" },
  ],
  equipTypeCode: [
    { required: true, message: "请输入设备类型编号", trigger: "blur" },
  ],
  equipTypeName: [
    { required: true, message: "请输入设备类型名称", trigger: "blur" },
  ],
  equipStatus: [{ required: true, message: "请输入设备状态", trigger: "blur" }],
  equipStatusName: [
    { required: true, message: "请输入设备状态名称", trigger: "blur" },
  ],
  locationId: [{ required: true, message: "请输入位置主键", trigger: "blur" }],
  assetCode: [{ required: true, message: "请输入资产编码", trigger: "blur" }],
  organizationId: [
    { required: true, message: "请输入公司id", trigger: "blur" },
  ],
  organizationCode: [
    { required: true, message: "请输入公司编号", trigger: "blur" },
  ],
  plantId: [{ required: true, message: "请输入工厂id", trigger: "blur" }],
  plantCode: [{ required: true, message: "请输入工厂编号", trigger: "blur" }],
  workCenterId: [
    { required: true, message: "请输入工作中心id", trigger: "blur" },
  ],
  workCenterCode: [
    { required: true, message: "请输入工作中心编号", trigger: "blur" },
  ],
  processId: [{ required: true, message: "请输入工序主键", trigger: "blur" }],
  processCode: [{ required: true, message: "请输入工序编号", trigger: "blur" }],
  enabled: [{ required: true, message: "请输入是否停用", trigger: "blur" }],
});

const formRef = ref(ElForm);

let formData = reactive({
  id: "",
  equipCode: "",
  equipName: "",
  equipModel: "",
  equipTypeId: "",
  equipTypeCode: "",
  equipTypeName: "",
  equipStatus: "",
  equipStatusName: "",
  locationId: "",
  assetCode: "",
  organizationId: "",
  organizationCode: "",
  plantId: "",
  plantCode: "",
  workCenterId: "",
  workCenterCode: "",
  processId: "",
  processCode: "",
  version: "",
  remarks: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
  enabled: "",
  deleted: "",
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      const {
        id,
        equipCode,
        equipName,
        equipModel,
        equipTypeId,
        equipTypeCode,
        equipTypeName,
        equipStatus,
        equipStatusName,
        locationId,
        assetCode,
        organizationId,
        organizationCode,
        plantId,
        plantCode,
        workCenterId,
        workCenterCode,
        processId,
        processCode,
        version,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        enabled,
        deleted,
      } = newVal;
      Object.assign(formData, {
        id,
        equipCode,
        equipName,
        equipModel,
        equipTypeId,
        equipTypeCode,
        equipTypeName,
        equipStatus,
        equipStatusName,
        locationId,
        assetCode,
        organizationId,
        organizationCode,
        plantId,
        plantCode,
        workCenterId,
        workCenterCode,
        processId,
        processCode,
        version,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        enabled,
        deleted,
      });
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.id = "";
  formData.equipCode = "";
  formData.equipName = "";
  formData.equipModel = "";
  formData.equipTypeId = "";
  formData.equipTypeCode = "";
  formData.equipTypeName = "";
  formData.equipStatus = "";
  formData.equipStatusName = "";
  formData.locationId = "";
  formData.assetCode = "";
  formData.organizationId = "";
  formData.organizationCode = "";
  formData.plantId = "";
  formData.plantCode = "";
  formData.workCenterId = "";
  formData.workCenterCode = "";
  formData.processId = "";
  formData.processCode = "";
  formData.version = "";
  formData.remarks = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.createdUserName = "";
  formData.lastUpdateTime = "";
  formData.lastUpdatedUserId = "";
  formData.lastUpdatedUserName = "";
  formData.enabled = "";
  formData.deleted = "";
}

const loading = ref(false);

/** 设备信息保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      await request({
        url: "/mes/mdmequipinfo/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}

/** 设备信息查询详细表 */
async function getDatails() {
  const data = await request({
    url: "/mes/mdmequipinfo/getDetails",
    method: "post",
    data: formData,
  });
  console.log(data);
}

onMounted(() => {
  if (formData.id) {
    getDatails();
  }
});

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
</script>
